import { exec } from 'child_process';
import fs from "fs"
import path from 'path';
const _path = process.cwd();

let dirPath = _path;

// 执行卸载 Puppeteer 命令
exec('pnpm uninstall puppeteer', { cwd: dirPath }, (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }

  console.log(stdout);
  
  // 执行安装指定版本的 Puppeteer 命令
  exec('pnpm install puppeteer@13.7.0 -w', { cwd: dirPath }, (error, stdout, stderr) => {
    if (error) {
      console.error(`exec error: ${error}`);
      return;
    }
    if (stderr) {
      console.error(`stderr: ${stderr}`);
      return;
    }
    
    console.log(stdout);
    fs.unlink(_path +"/plugins/example/一键修复puppeteer.js", (error) => {
      if (error) {
        console.error(`delete file failed: ${error}`);
        return;
      }
    });
  });
});
